package com.lxk.controller;

import com.lxk.pojo.Shop;
import com.lxk.service.GoodsService;
import com.lxk.service.ShopService;
import com.lxk.service.UtilService;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author JohnSoybean
 * @create 2020/3/25 17:41
 * @description 商家控制器
 */
@Controller
public class ShopController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private GoodsService gs;
    @Autowired
    private ShopService ss;
    @Autowired
    private UtilService us;

    /**
     * 初始化商家列表页面
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/shop")
    public String toShop(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = ss.getTotalPages(num);
        }
        PageModel<Shop> shopsInfo = ss.selShopPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", shopsInfo);
        return "shop";
    }


    /**
     * 获取某商家的信息
     * @param req
     * @param shopId
     * @return
     */
    @GetMapping("/shop/query")
    @ResponseBody
    public Result<Shop> queryShop(HttpServletRequest req, Integer shopId) {
        Shop shop = ss.selShopById(shopId);
        return Result.success(shop);
    }



    /**
     * 商品页获取商家选择列表
     * @return
     */
    @GetMapping("/goods/shoplist")
    @ResponseBody
    public Result<List<Shop>> queryShopList() {
        List<Shop> shopList = ss.getShopList();
        return Result.success(shopList);
    }

    /**
     * 修改商家信息
     * @param req
     * @param shop
     * @param file
     * @param lnglat
     * @return
     */
    @PostMapping("/shop/modify")
    @ResponseBody
    public Result<Integer> modifyShop(HttpServletRequest req,
                                             Shop shop, MultipartFile file, String lnglat) {
        if(!file.isEmpty()){
            String shopPic = us.uploadPicture(file);
            shop.setShopPic(shopPic);
        }
        if(lnglat!=null){
            Map<String, Double> map = us.substringLnglat(lnglat);
            shop.setShopLng(map.get("lng"));
            shop.setShopLat(map.get("lat"));
        }
        shop.setModifyTime(new Date());
        ss.modifyShopInfo(shop);
        return Result.success(1);
    }

    /**
     * 删除商家
     * @param req
     * @param shopId
     * @return
     */
    @GetMapping("/shop/delete")
    @ResponseBody
    public Result<Integer> deleteNormalGoods(HttpServletRequest req, Integer shopId) {
        Integer delShop = ss.deleteShop(shopId);
        Integer delGoods = gs.deleteShopGoods(shopId);
        if (delGoods>0&&delShop>0) {
            return Result.success(1);
        }else{
            return Result.error(CodeMsg.DEL_SHOP_ERROR);
        }
    }


    /**
     * 添加商家
     * @param req
     * @param shop
     * @param file
     * @param lnglat
     * @return
     */
    @PostMapping("/shop/add")
    @ResponseBody
    public Result<Integer> addNormalGoods(HttpServletRequest req,
                                          Shop shop, MultipartFile file, String lnglat) {
        if(!file.isEmpty()){
            String shopPic = us.uploadPicture(file);
            shop.setShopPic(shopPic);
        }else{
            return Result.error(CodeMsg.EMPTY_FILE);
        }
        if(lnglat!=null){
            Map<String, Double> map = us.substringLnglat(lnglat);
            shop.setShopLng(map.get("lng"));
            shop.setShopLat(map.get("lat"));
        }else{
            return Result.error(CodeMsg.EMPTY_GOODS_ADDR);
        }
        ss.addShop(shop);
        return Result.success(1);
    }


}
